#ifndef RPCPROVIDER_H
#define RPCPROVIDER_H

#pragma once
#include <string>
#include <google/protobuf/service.h>
#include <muduo/net/EventLoop.h>
#include <unordered_map>
#include "zookeeperutil.h"
class RpcProvider
{
public:
    RpcProvider(std::string ip, uint16_t port);
    ~RpcProvider();
    void subscribe(google::protobuf::Service* service);
    void run();
private:
    ZkClient zk_;
    
    struct ServiceInfo {
        google::protobuf::Service* service_;
        std::map<std::string, const google::protobuf::MethodDescriptor*> methodMap_;
    };
    using ServiceMap = std::unordered_map<std::string, const ServiceInfo>;
    ServiceMap serviceMap_;

    std::string ip_;
    uint16_t port_;

    muduo::net::EventLoop eventLoop_;

    void onConn(const muduo::net::TcpConnectionPtr& conn);
    void onMessage(const muduo::net::TcpConnectionPtr&,
                        muduo::net::Buffer*,
                        muduo::Timestamp);

    void rpcCall(const muduo::net::TcpConnectionPtr& conn, google::protobuf::Message*);

};

#endif